package com.lib.krismile.config;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;


import androidx.multidex.MultiDexApplication;

import com.blankj.utilcode.util.Utils;
import com.bumptech.glide.Glide;
import com.guoxiaoxing.phoenix.picker.Phoenix;
import com.lib.krismile.R;
import com.lib.krismile.http.data.HttpBaseResponse;
import com.lib.krismile.http.httptool.HttpException;
import com.lib.krismile.http.request.HttpFactory;
import com.lib.krismile.http.request.ServerAddress;
import com.lib.krismile.manager.MyActivityManager;
import com.orhanobut.hawk.Hawk;
import com.qmuiteam.qmui.util.QMUIActivityLifecycleCallbacks;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;


/**
 * @author : wangJiawei
 * @date : 2020/2/19 11:30
 * @desc :application
 */

public class App extends MultiDexApplication {
    private static Context context;
    public static boolean firstOpen;

    @Override
    public void onCreate() {
        super.onCreate();
        firstOpen = true;
        context = this;
        initActivityManager();
        init();
    }

    public static Context getContext() {
        return context;
    }

    /**
     * 管理Activity
     */
    private void initActivityManager() {
        registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            }

            @Override
            public void onActivityStarted(Activity activity) {
            }

            @Override
            public void onActivityResumed(Activity activity) {
                MyActivityManager.getInstance().setCurrentActivity(activity);
            }

            @Override
            public void onActivityPaused(Activity activity) {
            }

            @Override
            public void onActivityStopped(Activity activity) {
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            }

            @Override
            public void onActivityDestroyed(Activity activity) {
            }
        });
    }

    /**
     * 一些第三方库和本地代码的初始化设置
     */
    private void init() {
        Hawk.init(context).build();

        setHttpConfig();

        Phoenix.config()
                .imageLoader((mContext, imageView, imagePath, type) -> Glide.with(mContext)
                        .load(imagePath)
                        .into(imageView));

        //初始化 大佬的工具类utils
        Utils.init(this);
    }

    /**
     * 请求配置
     */
    public static void setHttpConfig() {

        HttpFactory.HTTP_HOST_URL = ServerAddress.getApiDefaultHost();
        HttpFactory.httpResponseInterface = (gson, response, type) -> {


            HttpBaseResponse httpResponse = gson.fromJson(response, HttpBaseResponse.class);
            if (httpResponse.errorCode != 0) {
                throw new HttpException(httpResponse.errorCode, httpResponse.errorMsg);
            }
            if (type.toString().contains("HttpBaseResponse")) {
                return gson.toJson(httpResponse);
            }
            return gson.toJson(httpResponse.data);
        };
    }

    /*
     * 设置上拉加载和下拉刷新的样式
     */
    static {
        // 设置全局的 Header 构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> {
            // new MaterialHeader(context).setColorSchemeColors(ContextCompat.getColor(context, R.color.common_accent_color))
            //全局设置主题颜色
            layout.setPrimaryColorsId(R.color.white, android.R.color.black);
            return new ClassicsHeader(context);
        });
        // 设置全局的 Footer 构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> new ClassicsFooter(context).setDrawableSize(20));

        // 设置全局初始化器
        SmartRefreshLayout.setDefaultRefreshInitializer((context, layout) -> {
            // 刷新头部是否跟随内容偏移
            layout.setEnableHeaderTranslationContent(true)
                    // 刷新尾部是否跟随内容偏移
                    .setEnableFooterTranslationContent(true)
                    // 加载更多是否跟随内容偏移
                    .setEnableFooterFollowWhenNoMoreData(true)
                    // 内容不满一页时是否可以上拉加载更多
                    .setEnableLoadMoreWhenContentNotFull(false)
                    .setEnableAutoLoadMore(false)
                    // 仿苹果越界效果开关
                    .setEnableOverScrollDrag(true);
        });

    }
}
